package com.suptracking.dao.jpa;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.wsdl.Output;

import com.suptracking.dao.CarDao;
import com.suptracking.entity.Car;
import com.suptracking.entity.User;
@Stateless
public class JpaCarDao implements CarDao {

	@PersistenceContext(unitName="Suptracking")
    private EntityManager em;
	
	

	@Override
	public void addCar(Car car) {

		
		try {
			
			em.persist(car);
			

		}

		finally {
			
		

		}

	}
	@Override
	public void deleteCarbyId(long id) {
		

			Car cartoremove = em.find(Car.class, id);
			

			em.remove(em.merge(cartoremove));

		
		
	}

	@Override
	public Car getCarInfo(long _id) {
	Car return_car;
		
		try {
			
			return_car = em.find(Car.class, _id);
			

		}

		finally {
			
			

		}

		return return_car;
	}

	@Override
	public List<Car> getAllCar() {

		CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Car> query = cb.createQuery(Car.class);
        List<Car> All_Cars = em.createQuery(query).getResultList();		
		return All_Cars;
	}

	public Long getNumberOfCar() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
				CriteriaQuery<Long> query = cb.createQuery(Long.class);
				query.select(cb.count(query.from(Car.class)));
		return em.createQuery(query).getSingleResult();
	}
	@Override
	public void editCar(Car car) {
		em.merge(car);
		
	}
}
